<?php

namespace Users\Model;

//use Zend\Db\Adapter\Adapter;
//use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
//use Zend\XmlRpc\Value\String;

class UploadTable 
{
	protected $tableGateway;
	protected $uploadSharingTableGateway;
	
	public function __construct(TableGateway $tableGateway, TableGateway $uploadSharingTableGateway)
	{
		$this->tableGateway = $tableGateway;
		$this->uploadSharingTableGateway = $uploadSharingTableGateway;
	}
	public function saveUpload(Upload $upload)
	{
		$data = array(
				'filename' => $upload->filename,
				'label' => $upload->label,
				'user_id' => $upload->user_id,
			
		);
		$id = (int)$upload->id;
		if ($id == 0) {
			$this->tableGateway->insert($data);
		} else {
			if ($this->getUpload($id)) {
				$this->tableGateway->update($data, array('id' => $id));
			} else {
				throw new \Exception('User ID does not exist');
			}
		}
	}
	
	public function getUpload($id)
	{
		$id = (int) $id;
		$rowset = $this->tableGateway->select(array('id' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}
	
	public function getlastUpload()
	{
		//$rowset = array();
		$rowset = $this->tableGateway->getLastInsertValue();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('  '.var_dump($rowset));
		return $rowset;
	}
	
	public function getUploadfilename($filename)
	{
		$name = (string)$filename;
		//throw new \Exception("Could not find row $name");
		$rowset = $this->tableGateway->select(array('filename' => $name));
		$row = $rowset->current();
		//throw new \Exception("Could not find row $rowset");
		if (!$row) {
			//throw new \Exception("Could not find row $row");
			return NULL;
		}
		return $row;
	}
	
	public function fetchAll()
	{
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}
	
	public function deleteUpload($id)
	{
		$this->tableGateway->delete(array('id' => $id));
	}
	
	public function getUploadsByUserId($userId)
	{
		$userId = (int) $userId;
		$rowset = $this->tableGateway->select(
				array('user_id' => $userId));
		return $rowset;
	}
	
	//**************************shering*******************************************
	public function getSheredUpload($id)
	{
		$id = (int) $id;
		$rowset = $this->uploadSharingTableGateway->select(array('id' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}
	
	public function addSharing($uploadId, $userId, $shared_by)
	{
		$data = array(
				'upload_id' => (int)$uploadId,
				'user_id' => (int)$userId,
				'shared_by' => (int)$shared_by,
		);
		$this->uploadSharingTableGateway->insert($data);
	}
	
	public function removeSharing($id)
	{
		//$data = array(
		//		'upload_id' => (int)$uploadId,
		//		'user_id' => (int)$userId,
		//);
		
		//$this->uploadSharingTableGateway->delete($data);
		$this->uploadSharingTableGateway->delete(array('id' => (int) $id));
	}
	
	public function getSharedUsers($uploadId)
	{
		$uploadId = (int) $uploadId;
		$rowset = $this->uploadSharingTableGateway->select(
				//array('upload_id' => $uploadId));
				function (Select $select) use ($uploadId){
					$select->columns(array())->where(array('uploads_sharing.upload_id'=>$uploadId))->join('user', 'uploads_sharing.user_id = user.id');
				});
		return $rowset;
	}
		
	public function getSharedUploadsForUserId($UserId)
	{
		$userId = (int) $UserId;
		$select = new Select();
		$select->from('uploads_sharing')
		->where(array('uploads_sharing.user_id' => $userId))->
		join('user','uploads_sharing.shared_by = user.id','name')->
		join('uploads','uploads_sharing.upload_id = uploads.id');
		$rowset = $this->uploadSharingTableGateway->selectWith($select);
		/*$rowset = $this->uploadSharingTableGateway->select(
				function (Select $select) use ($userId){
					$select->columns(array())->where(array('uploads_sharing.user_id'=>$userId))
					->join('uploads', 'uploads_sharing.upload_id=uploads.id')
					->join('user', 'uploads_sharing.shared_by = user.id');
					});*/
		return $rowset;
	}
	
	public function getSharedUploadsmakebyme($User_Id)
	{
		$user_Id = (int) $User_Id;
		//$rowset = $this->uploadSharingTableGateway->select(array('shared_by' => $user_Id));
		//$rowset = $this->uploadSharingTableGateway->select()->where(array('shared_by' => $user_Id));
		$select = new Select();
		$select->from('uploads_sharing')
		->where(array('uploads_sharing.shared_by' => $user_Id))->
		join('user','uploads_sharing.user_id = user.id','name')->
		join('uploads','uploads_sharing.upload_id = uploads.id','filename');
		$rowset = $this->uploadSharingTableGateway->selectWith($select);
		
		/*$rowset = $this->uploadSharingTableGateway->select(
				function (Select $select) use ($user_Id){
					$select->columns(array())->where(array('uploads_sharing.shared_by'=>$user_Id))
					->join('user', 'uploads_sharing.user_id = user.id');
				});*/
				//$row = $rowset->current();
				//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('  '.var_dump($row));
				return $rowset;
	}
}

?>